package com.huawei.appgallery.common.media.activity;

import android.os.Build.VERSION;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager.OnPageChangeListener;
import com.huawei.appgallery.common.media.api.IImagePreviewProtocol;
import com.huawei.appgallery.common.media.widget.zoomview.PictrueFragment;
import com.huawei.appgallery.foundation.ui.framework.activity.AbstractBaseActivity;
import com.huawei.appgallery.foundation.ui.framework.widget.BounceViewPager;
import com.huawei.hmf.annotation.ActivityDefine;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import o.blc;
import o.ble;
import o.ble.g;
import o.ble.j;
import o.blf;
import o.blt;
import o.blt.b;
import o.cdz;
import o.ceh;
import o.PermissionChecker;
import o.cgs;
import o.cgy;
import o.ckv;
import o.evd;
import o.fiv;
import o.FragmentManager;
import o.kc;

@ActivityDefine(alias = "ImagePreview", protocol = IImagePreviewProtocol.class)
public class ImagePreviewActivity extends AbstractBaseActivity implements OnClickListener, ceh, b {
    private e ʻॱ;
    private int ʽॱ = 0;
    private LinearLayout ʾ;
    private ArrayList<String> ʿ = new ArrayList();
    private TextView ˈ;
    private String ˉ;
    private ExecutorService ˊˊ;
    private ArrayList<blc> ˊˋ = new ArrayList();
    private fiv ˊॱ = fiv.ॱ(this);
    private IImagePreviewProtocol ˋॱ;
    private BounceViewPager ˏॱ;
    OnPageChangeListener ͺ = new OnPageChangeListener(this) {
        final /* synthetic */ ImagePreviewActivity ˎ;

        {
            this.ˎ = r1;
        }

        public void onPageScrolled(int i, float f, int i2) {
        }

        public void onPageScrollStateChanged(int i) {
            Fragment fragment = (Fragment) this.ˎ.ʻॱ.instantiateItem(this.ˎ.ˏॱ, this.ˎ.ॱˊ);
            if (fragment instanceof PictrueFragment) {
                final PictrueFragment pictrueFragment = (PictrueFragment) fragment;
                this.ˎ.ᐝॱ.postDelayed(new Runnable(this) {
                    final /* synthetic */ AnonymousClass1 ˎ;

                    public void run() {
                        pictrueFragment.ॱ();
                    }
                }, 100);
            }
            this.ˎ.ॱˊ = i;
            if (this.ˎ.ॱˎ.getVisibility() == 8) {
                this.ˎ.ॱˎ.setVisibility(0);
            }
            this.ˎ.ᐝॱ.removeMessages(1);
            this.ˎ.ᐝॱ.sendMessageDelayed(this.ˎ.ᐝॱ.obtainMessage(1), 3000);
            this.ˎ.ˊᐝ();
        }

        public void onPageScrolled(int i) {
        }
    };
    private int ॱˊ = -1;
    private blt ॱˋ;
    private View ॱˎ;
    private d ᐝॱ;

    static class d extends Handler {
        private final WeakReference<ImagePreviewActivity> ˎ;

        private d(ImagePreviewActivity imagePreviewActivity) {
            this.ˎ = new WeakReference(imagePreviewActivity);
        }

        public void handleMessage(Message message) {
            ImagePreviewActivity imagePreviewActivity = (ImagePreviewActivity) this.ˎ.get();
            if (imagePreviewActivity == null) {
                blf.ˋ.ˊ("ForumImageActivity", "activity is null ");
                return;
            }
            switch (message.what) {
                case 1:
                    if (imagePreviewActivity.ॱˎ.getVisibility() == 0) {
                        imagePreviewActivity.ॱˎ.setVisibility(8);
                        return;
                    }
                    return;
                default:
                    return;
            }
        }
    }

    class e extends kc {
        final /* synthetic */ ImagePreviewActivity ˏ;

        public e(ImagePreviewActivity imagePreviewActivity, FragmentManager juVar) {
            this.ˏ = imagePreviewActivity;
            super(juVar);
        }

        public Fragment ˏ(int i) {
            return PictrueFragment.ˋ(ॱ(i));
        }

        public int getCount() {
            return this.ˏ.ˊˋ == null ? 0 : this.ˏ.ˊˋ.size();
        }

        private blc ॱ(int i) {
            blc o_blc = new blc();
            if (i < getCount()) {
                return (blc) this.ˏ.ˊˋ.get(i);
            }
            return o_blc;
        }
    }

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        if (ॱˊ()) {
            ͺ();
            ᐝॱ();
            return;
        }
        finish();
    }

    private boolean ॱˊ() {
        this.ˋॱ = (IImagePreviewProtocol) this.ˊॱ.ॱ();
        if (this.ˋॱ == null) {
            blf.ˋ.ˊ("ForumImageActivity", "ForumImageProtocol.protocol is null");
            return false;
        }
        this.ॱˊ = this.ˋॱ.getOffset();
        this.ˉ = this.ˋॱ.getSavePath();
        this.ˊˋ = this.ˋॱ.getImageBeans();
        if (cgs.ˋ(this.ˊˋ)) {
            blf.ˋ.ˊ("ForumImageActivity", "activity imageBeans is empty");
            return false;
        }
        this.ʽॱ = this.ˊˋ.size();
        return true;
    }

    private void ͺ() {
        requestWindowFeature(1);
        if (VERSION.SDK_INT >= 21) {
            getWindow().setStatusBarColor(getResources().getColor(17170444));
            getWindow().setNavigationBarColor(getResources().getColor(17170444));
            if (evd.ˎ()) {
                evd.ॱ(getWindow(), 1);
                evd.ˏ(getWindow(), 1);
            }
        }
        setContentView(j.ˊ);
        this.ॱˎ = findViewById(ble.b.ʼ);
        this.ʾ = (LinearLayout) findViewById(ble.b.ʾ);
        this.ˈ = (TextView) findViewById(ble.b.ˎˎ);
    }

    private void ᐝॱ() {
        if (this.ॱˊ < 0 || this.ॱˊ >= this.ˊˋ.size()) {
            this.ॱˊ = 0;
        }
        if (cdz.ˎ(this)) {
            try {
                Collections.reverse(this.ˊˋ);
            } catch (UnsupportedOperationException e) {
                blf.ˋ.ˊ("ForumImageActivity", "reverse list error");
            }
            this.ॱˊ = (this.ˊˋ.size() - 1) - this.ॱˊ;
        }
        this.ॱˎ.setVisibility(0);
        this.ˏॱ = (BounceViewPager) findViewById(ble.b.ʼॱ);
        this.ʻॱ = new e(this, ˏॱ());
        this.ˏॱ.setPageCount(this.ˊˋ.size());
        this.ˏॱ.setAdapter(this.ʻॱ);
        this.ˏॱ.setCurrentItem(this.ॱˊ);
        this.ʾ.setOnClickListener(this);
        this.ˏॱ.setOnPageChangeListener(this.ͺ);
        ˊᐝ();
        this.ᐝॱ = new d();
        this.ᐝॱ.sendMessageDelayed(this.ᐝॱ.obtainMessage(1), 3000);
    }

    public void onClick(View view) {
        if (view.getId() == ble.b.ʾ) {
            this.ॱˎ.setVisibility(8);
            this.ᐝॱ.removeMessages(1);
            if (PermissionChecker.ˋ(this, 16)) {
                blf.ˋ.ˋ("ForumImageActivity", "Storage Permission checked");
            } else {
                ʼॱ();
            }
        }
    }

    public void onRequestPermissionsResult(int i, String[] strArr, int[] iArr) {
        PermissionChecker.ˎ(this, i, strArr, iArr);
    }

    public void ˏ(int i, int i2) {
        if (i2 == 0) {
            ʼॱ();
        }
    }

    private void ʼॱ() {
        String ˊ = ((blc) this.ˊˋ.get(this.ॱˊ)).ˊ();
        if (this.ʿ.contains(ˊ)) {
            blf.ˋ.ˊ("ForumImageActivity", "Download ThreadPool has same image url");
        } else if (cgy.ˎ(this.ˉ)) {
            blf.ˋ.ˊ("ForumImageActivity", "savePath empty");
        } else {
            this.ʿ.add(ˊ);
            this.ॱˋ = new blt(getApplicationContext(), ˊ, this.ˉ);
            this.ॱˋ.ˏ(this);
            ˊˋ();
            this.ॱˋ.executeOnExecutor(this.ˊˊ, new Void[0]);
        }
    }

    public void ˏ(blt o_blt, String str, boolean z) {
        CharSequence string;
        this.ʿ.remove(str);
        if (z) {
            string = getString(g.ʻ);
        } else {
            string = getString(g.ʼ);
        }
        ckv.ˎ(string, 0);
    }

    public void onDestroy() {
        super.onDestroy();
        if (this.ˊˋ != null) {
            this.ˊˋ.clear();
        }
        try {
            if (this.ˊˊ != null) {
                this.ˊˊ.shutdown();
            }
        } catch (Throwable e) {
            blf.ˋ.ˋ("ForumImageActivity", "shutdown executor error", e);
        }
    }

    private void ˊᐝ() {
        this.ˈ.setText(getString(g.ˏ, new Object[]{Integer.valueOf(this.ॱˊ + 1), Integer.valueOf(this.ʽॱ)}));
    }

    private void ˊˋ() {
        if (this.ˊˊ == null || this.ˊˊ.isShutdown()) {
            this.ˊˊ = Executors.newSingleThreadExecutor();
        }
    }

    public void ᐝ() {
        if (this.ॱˎ.getVisibility() == 8) {
            this.ॱˎ.setVisibility(0);
            this.ᐝॱ.removeMessages(1);
            this.ᐝॱ.sendMessageDelayed(this.ᐝॱ.obtainMessage(1), 3000);
            return;
        }
        this.ॱˎ.setVisibility(8);
    }
}
